package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysMenuBranch;
import com.ruoyi.system.domain.form.SysMenuBranchForm;
import com.ruoyi.system.domain.params.SysMenuBranchParams;
import com.ruoyi.system.mapper.SysMenuBranchMapper;
import com.ruoyi.system.mapper.SysMenuMapper;
import com.ruoyi.system.service.ISysMenuBranchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 分支菜单权限Service业务层处理
 *
 * @author liuxing
 * @date 2021-01-23
 */
@Service
@Slf4j
public class SysMenuBranchServiceImpl implements ISysMenuBranchService {
    @Autowired
    private SysMenuBranchMapper sysMenuBranchMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;


    /**
     * 查询分支菜单权限
     *
     * @param menuId 分支菜单权限ID
     * @return 分支菜单权限
     */
    @Override
    public SysMenuBranch selectSysMenuBranchById(Long menuId) {
        return sysMenuBranchMapper.selectSysMenuBranchById(menuId);
    }

    /**
     * 查询分支菜单权限列表
     *
     * @param sysMenuBranchParams 分支菜单权限
     * @return 分支菜单权限
     */
    @Override
    public List<SysMenuBranch> selectSysMenuBranchList(SysMenuBranchParams sysMenuBranchParams) {
        return sysMenuBranchMapper.selectSysMenuBranchList(sysMenuBranchParams);
    }


    public int superInsertSysMenuBranch(SysMenuBranchForm sysMenuBranchForm, Boolean isChild) {
        int successNum = 0;
        sysMenuBranchForm.setCreateBy(SecurityUtils.getUsername());
        sysMenuBranchForm.setCreateTime(DateUtils.getNowDate());
        sysMenuBranchForm.setUpdateBy(SecurityUtils.getUsername());
        sysMenuBranchForm.setUpdateTime(DateUtils.getNowDate());
        if (sysMenuBranchForm.getParentId() == null) {
            sysMenuBranchForm.setParentId(0L);
        }
        if (sysMenuBranchForm.getParentId() == 0) {
            sysMenuBranchForm.setAncestors("0");
        }
        if (sysMenuBranchForm.getOrderNum() == null) {
            sysMenuBranchForm.setOrderNum(0);
        }
        SysMenuBranch sysMenuBranch = new SysMenuBranch();
        BeanUtils.copyProperties(sysMenuBranchForm, sysMenuBranch);

        successNum += sysMenuBranchMapper.insertSysMenuBranch(sysMenuBranch);
        if (!isChild) {
            sysMenuBranchMapper.updateOrderNum(sysMenuBranch.getId(), sysMenuBranch.getParentId(), null, sysMenuBranch.getOrderNum());
        }
        log.info("sysMenuBranch.getId():" + sysMenuBranch.getId());
        //重新修改一遍是为了处理当原始菜单和当前菜单字段数据相同的时候清理为空
        SysMenuBranch sysMenuBranchNew = this.selectSysMenuBranchById(sysMenuBranch.getId());
        this.updateSysMenuBranch(sysMenuBranchNew);

        sysMenuBranchForm.setId(sysMenuBranch.getId());
        if (sysMenuBranchForm.getChildren() != null && sysMenuBranchForm.getChildren().size() > 0) {
            int childIndex = 0;
            for (SysMenuBranchForm child : sysMenuBranchForm.getChildren()) {
                childIndex++;
                child.setParentId(sysMenuBranch.getId());
                child.setAncestors(sysMenuBranchForm.getAncestors() + "," + sysMenuBranchForm.getId());
                child.setOrderNum(childIndex);
                successNum += superInsertSysMenuBranch(child, true);
            }
        }
        return successNum;
    }

    /**
     * 新增分支菜单权限
     *
     * @param sysMenuBranchForm 分支菜单权限
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysMenuBranch(SysMenuBranchForm sysMenuBranchForm) {
        int successNum = superInsertSysMenuBranch(sysMenuBranchForm, false);

        return successNum;
    }


    /**
     * 修改分支菜单权限
     *
     * @param sysMenuBranch 分支菜单权限
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysMenuBranch(SysMenuBranch sysMenuBranch) {
        SysMenuBranch sysMenuBranchOld = sysMenuBranchMapper.selectSysMenuBranchById(sysMenuBranch.getId());
        sysMenuBranch.setUpdateBy(SecurityUtils.getUsername());
        sysMenuBranch.setUpdateTime(DateUtils.getNowDate());
        if (sysMenuBranch.getOrderNum() != null) {
            Long parentId = sysMenuBranch.getParentId() != null ? sysMenuBranch.getParentId() : sysMenuBranchOld.getParentId();

            if (parentId.equals(sysMenuBranchOld.getParentId())) {
                sysMenuBranchMapper.updateOrderNum(sysMenuBranch.getId(), parentId, sysMenuBranchOld.getOrderNum(), sysMenuBranch.getOrderNum());
            } else if (!parentId.equals(sysMenuBranchOld.getParentId())) {
                //当父级别发送变动的时候  老的排序就不起作用了
                sysMenuBranchMapper.updateOrderNum(sysMenuBranch.getId(), parentId, null, sysMenuBranch.getOrderNum());
            }

        }

        int success = sysMenuBranchMapper.updateSysMenuBranch(sysMenuBranch);
        //当父类发生变化的时候祖籍也需要发生变化
        if (sysMenuBranch.getParentId() != null && !sysMenuBranch.getParentId().equals(sysMenuBranchOld.getParentId())) {
            String ancestorsNew;
            if (sysMenuBranch.getParentId() != 0) {
                SysMenuBranch sysMenuBranchParent = sysMenuBranchMapper.selectSysMenuBranchById(sysMenuBranch.getParentId());

                ancestorsNew = sysMenuBranchParent.getAncestors() + "," + sysMenuBranchParent.getId();
            } else {
                ancestorsNew = "0";
            }
            sysMenuBranchMapper.updateAncestors(sysMenuBranch.getId(), sysMenuBranchOld.getAncestors(), ancestorsNew);

        }
/*        //修改排序
        if(sysMenuBranch.getOrderNum()!=null){
            //当序号增加的时候
            if(sysMenuBranch.getOrderNum()>sysMenuBranchOld.getOrderNum()){

            }else if(sysMenuBranch.getOrderNum()>sysMenuBranchOld.getOrderNum()){//当序号降低

            }
        }*/


        return success;
    }

    /**
     * 批量删除分支菜单权限
     *
     * @param ids 需要删除的分支菜单权限ID
     * @return 结果
     */
    @Override
    public int deleteSysMenuBranchByIds(Long[] ids) {
        int successNum = 0;
        for (Long menuId : ids) {
            successNum += deleteSysMenuBranchById(menuId);
        }
        return successNum;
    }

    /**
     * 删除分支菜单权限信息
     *
     * @param id 分支菜单权限ID
     * @return 结果
     */
    @Override
    public int deleteSysMenuBranchById(Long id) {

        //递归删除子菜单
        return sysMenuBranchMapper.deleteSysMenuBranchById(id);
    }

    /**
     * 启用分支菜单权限
     *
     * @param id 分支菜单权限ID
     * @return 结果
     */
    @Override
    public int enableSysMenuBranch(Long id) {
        SysMenuBranch sysMenuBranch = new SysMenuBranch();
        sysMenuBranch.setId(id);
        sysMenuBranch.setStatus("0");
        return sysMenuBranchMapper.updateSysMenuBranch(sysMenuBranch);
    }

    /**
     * 停用分支菜单权限
     *
     * @param id 分支菜单权限ID
     * @return 结果
     */
    @Override
    public int stopSysMenuBranch(Long id) {
        SysMenuBranch sysMenuBranch = new SysMenuBranch();
        sysMenuBranch.setId(id);
        sysMenuBranch.setStatus("1");
        return sysMenuBranchMapper.updateSysMenuBranch(sysMenuBranch);
    }
}
